package li.teng.android.xplanet;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.location.Location;
import android.util.Log;

public class XPlanetClient {
	public static String REGISTER = "SHTeacherServer";
	public static String UPLOAD = "UPLOAD";
	public static String QUERY = "TeacherQuery";
	public static String GETSTATUS = "GetUserStatus";
	public static String REPORT_POSITION = "GetUserStatus";
	public static String BASEURL = "http://goxplanet.appspot.com/";
	String m_MyId = null;
	
	public XPlanetClient(String sim_id){
		m_MyId = sim_id;
	}
	public String request(String url){
	    	String ret = "";
	    	try
	    	{
		    	HttpClient hc = new DefaultHttpClient();
		    	//String url = "http://goxplanet.appspot.com/SHTeacherServer";
		    	//url += "?id=" + sim + "&msg=" + ((EditText)findViewById(R.id.input)).getText();
		    	HttpGet post = new HttpGet(url);
		    	Log.w("XPlanet", "request = " + url);
		    	HttpResponse rp = hc.execute(post);
		    	if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
		    		ret = EntityUtils.toString(rp.getEntity(),"UTF-8");
	    	}catch(Exception e){
	    		Log.w("XPlanet", e.toString());
	    	}
	    	return ret;
	}
	
	public String registerClient(String name){
		String url = BASEURL + "PService?action=register&sim=" + m_MyId + "&name=" + name;
		return request(url); 
	}
	public String ReportPosition(Location location){
		if(m_MyId == null){
			m_MyId = "1";
		}
		String url = BASEURL + "PService?action=update&id=" + m_MyId 
		+ "&logitude=" + location.getLongitude() + "&latitude=" + location.getLatitude();
		return request(url); 
	}
}
